<?php
declare (strict_types=1);

namespace app\admin\model\promotion;

use app\common\model\BaseModel;
use app\common\model\ProductType;
use app\course\model\base\admin\Course;
use think\model\concern\SoftDelete;

/**
 * Class Promotion
 *
 * @package app\admin\model\promotion
 */
class Promotion extends BaseModel
{
    use SoftDelete;

    /**
     * 表名
     *
     * @var string
     */
    protected $name = 'promotion';

    /**
     * 设置自动写入时间
     *
     * @var boolean
     */
    protected $autoWriteTimestamp = true;

    /**
     * 时间显示格式
     *
     * @var string
     */
    protected $dateFormat = 'Y-m-d H:i';

    /**
     * 定义表主键
     *
     * @var string
     */
    protected $pk = 'id';

    protected $type = [
        'start_time' => 'timestamp',
        'end_time'   => 'timestamp'
    ];

    /**
     * 定义版块状态
     *
     * @var string[]
     */
    protected $statusList = [
        0 => '禁用',
        1 => '启用'
    ];

    /**
     * 定义类型
     *
     * @var string[]
     */
    protected $productType = [
        'video'   => '点播',
        'live'    => '直播',
        'classes' => '班级'
    ];
    /**
     * 产品类型key
     *
     * @var string[]
     */
    protected $productTypeKey = [
        1 => 'video',
        2 => 'live',
        4 => 'classes'
    ];

    /**
     * 全局查询范围
     *
     * @var [type]
     */
    protected $globalScope = ['mhm'];

    /**
     * 限制机构条件
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-16
     *
     * @param    [type]                         $query [description]
     */
    public function scopeMhm($query)
    {
        $query->where('mhm_id', request()->mhm_id);
    }

    /**
     * 获取活动状态
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/5/7
     *
     * @param $value
     * @param $data
     * @return int
     */
    public function getActiveStatusAttr($value, $data)
    {
        $time = \think\facade\Request::time();
        if ($data['status'] == 1) {
            // 活动未被禁用
            if ($data['start_time'] > $time) {
                // 活动未开始
                $status = 2;
            } else if ($data['end_time'] < $time) {
                // 活动已结束
                $status = 3;
            } else if ($data['start_time'] < $time && $data['end_time'] > $time) {
                $status = 1;
            }
        } else {
            // 活动被禁用
            $status = 0;
        }

        return $status;
    }

    /**
     * 获取产品类型名称
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/4/15
     *
     * @param $value
     * @param $data
     * @return mixed|string
     */
    public function getProductTypeTextAttr($value, $data)
    {
        $key = $this->productTypeKey[$data['product_type_id']] ?? '';
        if ($key) {
            return ProductType::getProductTitleByKey($key);
        }

        return '未知';
    }

    /**
     * 获取状态列表
     *
     * @return string[]
     */
    public function getStatusList(): array
    {
        return $this->statusList;
    }

    /**
     * @return string[]
     */
    public function getProductType(): array
    {
        return $this->productType;
    }

    /**
     * 课程信息
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/4/15
     *
     * @return \think\model\relation\BelongsTo
     */
    public function courseAttach()
    {
        return $this->belongsTo(Course::class, 'product_id')->with(['coverUrl']);
    }

}